/*
 *  color_nota.cpp
 *  Wilson
 *
 *  Created by Sebastian Orfino on 11/22/11.
 *  Copyright 2011 lbs3. All rights reserved.
 *
 */
#include "VCPlugInHeaders.h"

//SDK includes
#include "IWorkspace.h"
#include "ISwatchUtils.h"
#include "ISwatchList.h"
#include "IRenderingObject.h"
#include "IGraphicStateUtils.h"
#include "IGraphicStateRenderObjects.h"
#include "IColorOverrides.h"
#include "IColorSystemUtils.h"
#include "IPersistUIDData.h"
#include "IPMInkBossData.h"
#include "IGraphicAttrRealNumber.h"
#include "SwatchLibPanelID.h"


//project includes
#include "color_nota.h"

#define kWilPrefijoSwatch "WilSwatch_"

using namespace wilson;

void color_nota::crear(const ColorArray& color, bool esInvisible)
{
	swatch_ = kInvalidUID;
	InterfacePtr<ISwatchList> iSwatchList(Utils<ISwatchUtils> ()->QueryActiveSwatchList());

	//atencion que color DEBE SER RGB.
	const int16	colorSpace = kPMCsCalRGB;
	
	PMString sufijo = Utils<ISwatchUtils>()->FormatSwatchNameWithColorValue(iSwatchList, colorSpace, color, kFalse);
	PMString nombre(kWilPrefijoSwatch);
	nombre.Append(sufijo);
	
	//primero me fijo si existia o no. Si existia tomo la referencia a ese.
	UIDRef swatch = iSwatchList->FindSwatch(nombre);
	if (swatch != kInvalidUIDRef) {
		swatch_ = swatch.GetUID();
		return; //ya existia.
	}
	
	//si no existia en el siguiente codigo se crea el swatch.
	const IInkData::InkType	inkType = IInkData::kSpotInk;
	const int32	creatorID=kPantoneCoatedNewLibCreatorID; //este creator ni idea si esta bien, creo que no importa.
	
	InterfacePtr<IRenderingObject>	iNewRenderObject (Utils<IGraphicStateUtils> ()->CreateTemporaryRenderObject (kPMColorBoss));
	InterfacePtr<IColorData> 	iNewColorData( iNewRenderObject, UseDefaultIID () );
	InterfacePtr<IInkData> 		iNewInkData( iNewRenderObject, UseDefaultIID () );
	if (iNewColorData)
		iNewColorData->SetColorData (colorSpace, color);
	if (iNewRenderObject)
	{
		iNewRenderObject->SetVisibility (!esInvisible);
		iNewRenderObject->SetSwatchName (nombre);
		iNewRenderObject->SetCanDelete (kTrue);
		iNewRenderObject->SetCreatorID (creatorID);
	}
	if (iNewInkData)
		iNewInkData->SetInkType (inkType);
	
	Utils<ISwatchUtils>			iSwatchUtils;
	InterfacePtr<IWorkspace>	workspacePtr (iSwatchList, IID_IWORKSPACE );
	if (iSwatchUtils)
		swatch_ = iSwatchUtils->CreateNewSwatch (kPMColorBoss, iNewRenderObject, workspacePtr, kFalse); 
	//  Simply adding the selected swatches to the swatches panel should not affect
	//  the graphic state. Unless explicitly selected in the swatches panel, 
	//  user don't need to set the last para to kTrue to update the graphics state
	
	
	
	
	
}